import Data.List 
import System.IO

parseInput :: String -> [Int]
parseInput = map read . lines 

findSum2020 :: [Int] -> Maybe (Int, Int)
findSum2020 []  = Nothing
findSum2020 [x] = Nothing
findSum2020 (x:xs) = 
    if reminderExits 
        then Just (x,reminder)
        else findSum2020 xs
    where
        reminder = 2020 - x
        reminderExits = elem reminder xs

outputValue :: Maybe (Int, Int) -> Int
outputValue result = 
    case result of 
        Just (x, y) -> x * y
        Nothing -> 0

main :: IO ()
main = do
    input <- readFile "./day01/input.txt"

    print . outputValue . findSum2020 . parseInput $ input